package it.gameplanet.client.widgets.news;

import it.gameplanet.client.GamePlanetContext;
import it.gameplanet.client.utils.AbstractAsyncCallback;
import it.gameplanet.client.widgets.support.ContextMenuLabel;
import it.gameplanet.model.News;
import it.gameplanet.service.GamePlanetService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.StackPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class NewsPanel extends Composite {

	final VerticalPanel vPanelNews = new VerticalPanel();
	{
		vPanelNews.addStyleName("gwt-FullWidthAndHeight");
	}
	
	final EditNewsDialog insertDialog = new EditNewsDialog(new Command() {
		@Override
		public void execute() {
			loadNews();
		}
	});

	public NewsPanel() {
		super();
		
		final VerticalPanel vPanel = new VerticalPanel();
		
		if(GamePlanetContext.isAdminUser()) {
			Button addNewsButton = new Button("Aggiungi News");
			addNewsButton.addClickHandler(new ClickHandler(){

				@Override
				public void onClick(ClickEvent event) {
					insertDialog.editNews(new News());
				}
			});
			vPanel.add(addNewsButton);
		}		

		vPanel.add(vPanelNews);
		
		StackPanel stackPanel = new StackPanel();
		stackPanel.add(vPanel, "Home Page");
		stackPanel.addStyleName("gwt-FullWidthAndHeight");
		
		initWidget(stackPanel);
	}

	
	
	private void loadNews() {
		GamePlanetService.Utils.getGamePlanetServiceAsync().findAllNews(new AbstractAsyncCallback<List<News>>() {

			class ConsumeBoolean {
				private boolean value = true;
				
				public boolean value() {
					try {
						return value;						
					}
					finally {
						value = false;
					}
				}
 
			}
			
			public void onSuccess(List<News> result) {

				ConsumeBoolean isOpenedPanel = new ConsumeBoolean();
				
				vPanelNews.clear();
				
				for(final News news: result) {
					
					Command editCommand = new Command() {
						@Override
						public void execute() {
							insertDialog.editNews(news);
						}
					};
					
					Command deleteCommand = new Command() {
						@Override
						public void execute() {
							loadNews();
						}
					};

					Command insertCommand = new Command() {
						@Override
						public void execute() {
							insertDialog.editNews(new News());
						}
					};

					final Map<String, Command> menu = new HashMap<String, Command>(2);
					menu.put("Elimina News", deleteCommand);
					menu.put("Modifica News", editCommand);
					menu.put("Inserisci News", insertCommand);
					
					Grid gridTitolo = new Grid(1,2);
					gridTitolo.setWidget(0,0, new ContextMenuLabel(news.getTitolo(), menu));
					gridTitolo.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_LEFT);
					Label labelData =new Label(DateTimeFormat.getShortDateFormat().format(news.getDataPubblicazione()));
					labelData.addStyleName("gwt-Data-Pubblicazione");
					gridTitolo.setWidget(0,1, labelData);
					gridTitolo.getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_RIGHT);
					gridTitolo.addStyleName("gwt-FullWidthAndHeight");
					gridTitolo.setWidth("400px");
					
					DisclosurePanel newsPanel = new DisclosurePanel(gridTitolo);
					newsPanel.setAnimationEnabled(true);
					newsPanel.setContent(new HTML(news.getTesto()));
					newsPanel.setOpen(isOpenedPanel.value());
					newsPanel.addStyleName("gwt-FullWidthAndHeight");
					
					vPanelNews.add(newsPanel);
				}
			}
		});
	}



	@Override
	protected void onLoad() {
		loadNews();
	}
}
